
<style lang="scss" scoped>
.page-wrap{
  background: #f8f8f8;
}
.page-container{
  padding: 60rpx 30rpx;
}
.card-box{
  width: 690rpx;
  border-radius: 20rpx;
  box-shadow: rgba(50,50,50,.1) 0 0 30rpx 0;
  background: #fff;
  overflow: hidden;
}

.action-box{
  padding: 30rpx 50rpx;
}

.u-progress-content {

}

.run-num{
  display: flex;
  justify-content: center;
  padding: 50rpx 0;
}

.u-progress-dot {
  width: 16rpx;
  height: 16rpx;
  border-radius: 50%;
  background-color: #fb9126;
}
.u-progress-title{
  color: #999999;
  font-size: 24rpx;
  padding-bottom: 12rpx;
}
.u-progress-info {
  font-size: 38rpx;
  font-weight: bold;
  color: #333;
  letter-spacing: 2rpx;
  text-align: center;
}
.task-box{
  width: 690rpx;
  height: 400rpx;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  &__text{
    position: relative;
    z-index: 4;
    color: #fff;
    font-size: 36rpx;
  }
  &__mask{
    background: rgba(0,0,0,.4);
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 3;
  }
  &__thumb{
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 2;
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
  }
}
</style>
<template>
  <div class="page-wrap">
<!--    <u-cell-group>-->
<!--      <u-cell-item v-for="(row,idx) in wxRunData" :key="idx" :title="row.day" :value="row.step+'步'"></u-cell-item>-->
<!--    </u-cell-group>-->
    <div class="page-container" v-show="ready">
      <div class="card-box">
        <div class="task-box" v-if="task_info.id">
          <div class="task-box__text">{{task_info.title}}</div>
          <div class="task-box__mask"></div>
          <div class="task-box__thumb" :style="{backgroundImage:'url('+task_info.thumb+')'}"></div>
        </div>
        <div class="run-num">
          <u-circle-progress active-color='#06ad56' :percent="100" duration="2000">
            <view class="u-progress-content">
              <view class="u-progress-title">今日步数</view>
              <div class='u-progress-info'>{{ wxRunData.step }}</div>
            </view>
          </u-circle-progress>
        </div>
        <div class="action-box">
          <u-button  v-if="isTodayAllowDonate" type="success" @click="donateFn">立即捐赠</u-button>
          <div v-else-if="isDonate" class="text-center color-red fz-12 p-15">*您已参与该活动,今日无法重复捐赠</div>
          <div v-else class="text-center color-red fz-12 p-15">*该活动今日已无法参与</div>
          <!--task_info.percent-->
          <u-line-progress active-color='#06ad56' :striped="true" :striped-active="true" :percent="task_info.percent"></u-line-progress>
          <div class="c9 fz-12 p-15 text-center">该活动共需要<span class="color-green">{{task_info.require_num}}</span>积分,今日可捐赠剩余<span class="color-red">{{ task_info.allow_num_day }}</span>积分</div>
        </div>
      </div>
    </div>

  </div>
</template>
<script>
import { pageMixin } from '@/mixins'
import Promisify from '@/common/Promisify'
import { getWxRunData, getCurrentStepTask, donateStep } from '@/api'
import { checkIsLogin } from '@/common/helper'
import UCellItem from '@/uview-ui/components/u-cell-item/u-cell-item'
import UCellGroup from '@/uview-ui/components/u-cell-group/u-cell-group'
import UCircleProgress from '@/uview-ui/components/u-circle-progress/u-circle-progress'
import UButton from '@/uview-ui/components/u-button/u-button'
import { Exception } from '@/common/Exception'
import UEmpty from '@/uview-ui/components/u-empty/u-empty'
import { confirm, hideLoading, modal, showLoading, toast } from '@/common/fun'
import ULineProgress from '@/uview-ui/components/u-line-progress/u-line-progress'

export default {
  components: { ULineProgress, UEmpty, UButton, UCircleProgress, UCellGroup, UCellItem },
  mixins: [pageMixin],
  data () {
    return {
      ready: false,
      isDonate: false,
      donate_info: {},
      isTodayAllowDonate: false,
      task_info: {},
      wxRunData: { step: 0, timestamp: 0 }
    }
  },
  methods: {
    donateFn () {
      confirm({
        title: '提示',
        content: '每天可以捐赠一次，捐赠成功后随即为项目助力随机数量的大米,是否确认捐赠?'
      }).then(() => {
        donateStep({ task_id: this.task_info.id, step_num: this.wxRunData.step }, { tip: '捐赠中...' }).then(res => {
          this.isTodayAllowDonate = false
          this.isDonate = true
          toast('捐赠成功')
        }).catch(err => {
          Exception.handle(err)
        })
      }).catch(() => {

      })
    },
    async getWxRunDataNoLogin () {
      try {
        const checkSeting = await Promisify('getSetting').catch(() => {
          throw Error('获取配置失败')
        })
        // 未获得用户信息权限,需要让用户授权
        if (!checkSeting.authSetting['scope.werun']) {
          await Promisify('authorize', { scope: 'scope.werun' }).catch(() => {
            throw Error('请授权获取运动')
          })
        }
        const wxRunInfo = await Promisify('getWeRunData').catch(() => {
          throw Error('获取运动信息失败')
        })
        const { encryptedData, iv } = wxRunInfo
        const lp_raw_data = JSON.stringify({
          encryptedData,
          iv
        })
        var runReqdata = await getWxRunData({
          lp_raw_data
        }, { tip: '获取中' }).then(res => res.data).catch((err) => {
          throw Error(err.msg || '获取运动数据错误')
        })
        console.log(runReqdata)
        return runReqdata
      } catch (e) {
        return false
      }
    },
    async getWxRunDataLogin () {
      try {
        // 获取code
        const wxLoginRt = await Promisify('login').catch(() => {
          throw Error('微信login错误')
        })
        const { code: lp_code } = wxLoginRt

        const checkSeting = await Promisify('getSetting').catch(() => {
          throw Error('获取配置失败')
        })
        // 未获得用户信息权限,需要让用户授权
        if (!checkSeting.authSetting['scope.werun']) {
          await Promisify('authorize', { scope: 'scope.werun' }).catch(() => {
            throw Error('请授权获取运动')
          })
        }
        const wxRunInfo = await Promisify('getWeRunData').catch(() => {
          throw Error('获取运动信息失败')
        })
        const { encryptedData, iv } = wxRunInfo
        const lp_raw_data = JSON.stringify({
          encryptedData,
          iv
        })
        var runReqdata = await getWxRunData({
          code: lp_code,
          lp_raw_data
        }, { tip: '获取中' }).then(res => res.data).catch((err) => {
          throw Error(err.msg || '获取运动数据错误')
        })
        console.log(runReqdata)
        return runReqdata
      } catch (e) {
        return false
      }
    },
    async _init_func () {
      try {
        showLoading()
        const { task_info = {}, isTodayAllowDonate = false, isDonateToday = false } = await getCurrentStepTask().then(res => res.data).catch(err => {
          throw err
        })
        this.isDonate = isDonateToday
        this.task_info = task_info
        this.isTodayAllowDonate = isTodayAllowDonate

        let runData = await this.getWxRunDataNoLogin()
        if (runData === false) {
          runData = await this.getWxRunDataLogin()
        }
        if (runData !== false) {
          console.log(runData)
          this.wxRunData = runData.stepInfoList.reverse()[0]
        }

        hideLoading()
      } catch (e) {
        hideLoading()
        Exception.handle(e)
      }
      this.ready = true
    }
  },
  onShow () {
    if (checkIsLogin(1, 1)) {
      this._init_func()
    }
  },
  onLoad (options) {

  }
}
</script>
